package com.ekuaitu.kuaitustaff.baiduMap;

import android.text.TextUtils;
import android.widget.TextView;

import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.search.core.SearchResult;
import com.baidu.mapapi.search.geocode.GeoCodeResult;
import com.baidu.mapapi.search.geocode.GeoCoder;
import com.baidu.mapapi.search.geocode.OnGetGeoCoderResultListener;
import com.baidu.mapapi.search.geocode.ReverseGeoCodeOption;
import com.baidu.mapapi.search.geocode.ReverseGeoCodeResult;


/**
 * Created by Michael on 11/6.
 */

public class GeoCoderUtils {
    private static GeoCoderUtils instance;
    private GeoCoder mSearch;
    private OnGeoListener onGeoListener;
    private TextView textView;

    public static GeoCoderUtils getInstance() {
        if (instance == null) {
            instance = new GeoCoderUtils();
        }
        return instance;
    }

    public GeoCoderUtils() {
    }


    public void reverseGeoCode(LatLng position, OnGeoListener onGeoListener) {
        this.onGeoListener = onGeoListener;
        mSearch = GeoCoder.newInstance();
        mSearch.setOnGetGeoCodeResultListener(listener);
        mSearch.reverseGeoCode(new ReverseGeoCodeOption()
                .location(position));
    }

    public void reverseGeoCode(LatLng position, TextView textView) {
        this.textView = textView;
        mSearch = GeoCoder.newInstance();
        mSearch.setOnGetGeoCodeResultListener(listener);
        mSearch.reverseGeoCode(new ReverseGeoCodeOption()
                .location(position));
    }


    OnGetGeoCoderResultListener listener = new OnGetGeoCoderResultListener() {
        public void onGetGeoCodeResult(GeoCodeResult result) {
            //获取地理编码结果
            if (result == null || result.error != SearchResult.ERRORNO.NO_ERROR) {
            }

        }

        @Override
        public void onGetReverseGeoCodeResult(ReverseGeoCodeResult result) {
            //获取反向地理编码结果
            if (result == null || result.error != SearchResult.ERRORNO.NO_ERROR) {
                if (onGeoListener != null) {
                    onGeoListener.onGeoFailure();
                }
                if (textView != null) {
                    textView.setText("获取位置信息失败");
                }
            } else {
                if (onGeoListener != null) {

                    if (null != result.getPoiList() && !result.getPoiList().isEmpty() && null != result.getPoiList().get(0).address && !TextUtils.isEmpty(result.getPoiList().get(0).address.trim())) {
                        onGeoListener.onGeoSuccess(result.getPoiList().get(0).address, result.getCityCode() + "", result.getAddressDetail().city,result.getAdcode()+"");
                    } else {
                        if (null != result.getAddressDetail() && null != result.getAddressDetail().street && !TextUtils.isEmpty(result.getAddressDetail().street.trim()) && null != result.getAddressDetail().streetNumber && !TextUtils.isEmpty(result.getAddressDetail().streetNumber.trim())) {
                            onGeoListener.onGeoSuccess(result.getAddressDetail().street + result.getAddressDetail().streetNumber, result.getCityCode() + "", result.getAddressDetail().city,result.getAdcode()+"");
                        } else {
                            onGeoListener.onGeoSuccess(result.getAddress(), result.getCityCode() + "", result.getAddressDetail().city,result.getAdcode()+"");
                        }
                    }
                }
                if (textView != null) {
                    if (null != result.getPoiList() && !result.getPoiList().isEmpty() && null != result.getPoiList().get(0).address && !TextUtils.isEmpty(result.getPoiList().get(0).address.trim())) {
                        textView.setText(result.getPoiList().get(0).address);
                    } else {
                        if (null != result.getAddressDetail() && null != result.getAddressDetail().street && !TextUtils.isEmpty(result.getAddressDetail().street.trim()) && null != result.getAddressDetail().streetNumber && !TextUtils.isEmpty(result.getAddressDetail().streetNumber.trim())) {
                            textView.setText(result.getAddressDetail().street + result.getAddressDetail().streetNumber);
                        } else {
                            textView.setText(result.getAddress());
                        }
                    }
                }
            }
        }
    };

    public interface OnGeoListener {
        void onGeoSuccess(String address, String cityCode, String city,String adCode);

        void onGeoFailure();
    }

}
